<?php ###首页### ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>首页</title>
    <link rel="stylesheet" href="css/index.css" />
  </head>
  <body>
    <div class="header">
    <div class="main">
      <div class="title">
        <div class="left">
          <?php
            session_start();
            if(isset($_SESSION['userName'])){
                $userName = $_SESSION['userName'];
            }else{
                echo "<script>alert('您还未登陆');location.href='login.php';</script>";
            }
          ?>
          <h1>明明商城</h1>
          <h3><?php echo $userName;?></h3>
        </div>
         <div class="right">
          <a href="recommend.php" style="font-size:1rem;margin-right:10px;">今日爆款</a>
          <a href="shoppingCar.php" style="font-size:1rem;margin-right:10px;">购物车</a>
          <a href="orders.php" style="font-size:1rem;margin-right:10px;">我的订单</a>
          <form class="search" action="search.php" method="get">
          <select name="key">
              <option value="brand">品牌</option>
              <option value="name">名称</option>
              <option value="place">产地</option>
          </select>
          <input type="text" name="value" placeholder="请输入查找关键词">
          <input type="submit" value="搜索">
        </form>
       </div>
      </div>
      <div class="nav">
        <ul>
          <li><a href="index.php?id=1">手机类</a></li>
          <li><a href="index.php?id=2">数码类</a></li>
          <li><a href="index.php?id=3">厨卫类</a></li>
          <li><a href="index.php?id=4">健康类</a></li>
          <li><a href="index.php?id=5">影视类</a></li>
          <li><a href="index.php?id=6">生活类</a></li>
          <li><a href="index.php?id=7">办公类</a></li>
        </ul>
        
      </div>
      <div class="display">
        <?php
            require ("connect.php");
            if(isset($_GET['id'])){
                $id = $_GET['id'];
            }else{
                $id = 1;
            }
            if(isset($_GET['page'])){
                $currentPage = $_GET['page'];
            }else{
                $currentPage = 1;
            }
            $query = "select * from tb_shangpin where typeid = $id";
            $result = mysqli_query($link, $query) or die('查询失败'.mysqli_error());
            $total = mysqli_num_rows($result);
            $pageSize = 8;
            if($total % $pageSize == 0){
              $numOfPage = (int)($total / $pageSize);
            }else{
              $numOfPage = (int)($total / $pageSize) + 1;
            }
            $start = ($currentPage - 1) * $pageSize;
            $query2 = "select * from tb_shangpin where typeid = $id limit $start, $pageSize";
            $result2 = mysqli_query($link, $query2) or die('查询失败'.mysqli_error());
            while($row = mysqli_fetch_array($result2)){ 
        ?>
        <div class="goods">
            <a href="details.php?id=<?php echo $row['id'];?>&name=<?php echo $row['name'];?>">
                <img src="<?php echo $row['photo']; ?>" alt="" width="200" height="200"/>
            </a>
            <h3 class="price">￥<?php echo $row['vipPrice']; ?></h3>
            <h4 class="name"><?php echo $row['name']; ?></h4>
        </div>
        <?php
            }
            mysqli_close($link);
        ?>
      </div>
    </div>
    <div class="clear" style="clear:both;"></div>
    <div class="page">
        <p>共<?php echo $total;?>条记录&nbsp;&nbsp;目前第<?php echo $currentPage;?>页/共<?php echo $numOfPage;?>页</p>
        <?php
            if($numOfPage>1 && $currentPage>1){
                echo "<a href = 'index.php?id=$id&page=1'>首页</a>";
                echo "<a href = 'index.php?id=$id&page=".($currentPage-1)."'>上一页</a>";
            }
            if($numOfPage>1 && $currentPage<$numOfPage){
                echo "<a href = 'index.php?id=$id&page=".($currentPage+1)."'>下一页</a>";
                echo "<a href = 'index.php?id=$id&page=".$numOfPage."'>尾页</a>";
            }
        ?>
    </div>
  </body>
</html>
